package com.heima.user.controller.v1;

import com.heima.apis.article.IArticleClient;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.FollowDto;
import com.heima.user.service.ApUserService;
import com.heima.utils.thread.ApUserThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/api/v1/user")
@Api(value = "用户信息接口", tags = "用户信息接口")
@Slf4j
public class ApUserController {

    @Autowired
    private ApUserService apUserService;
    @Resource
    private IArticleClient articleClient;
    @Resource
    private IWemediaClient wemediaClient;

    @GetMapping("/{userId}")
    @ApiOperation("获取用户信息")
    public ResponseResult getUserInfo(@PathVariable Integer userId) {
        return apUserService.getUserInfo(userId);
    }

    @GetMapping("/{userId}/fans")
    @ApiOperation("获取用户粉丝列表")
    public ResponseResult getUserFansList(@PathVariable Integer userId,
                                          @RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer size) {
        return apUserService.getUserFansList(userId, page, size);
    }

    @GetMapping("/{userId}/follows")
    @ApiOperation("获取用户关注列表")
    public ResponseResult getUserFollowList(@PathVariable Integer userId,
                                            @RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer size) {

        return apUserService.getUserFollowList(userId, page, size);
    }
    @GetMapping("/isFollowed")
    @ApiOperation("判断当前用户是否已关注目标用户")
    public ResponseResult isFollowedByUser(@RequestParam Integer userId,
                                           @RequestParam Integer followId) {
        return apUserService.isFollowedByUser(userId, followId);
    }

    @PostMapping("/follow")
    @ApiOperation("关注或取消关注用户")
    public ResponseResult followUser(@RequestParam Integer userId,
                                     @RequestBody FollowDto followDto) {
        return apUserService.followUser(userId, followDto);
    }

    @GetMapping("/isFollowMe")
    @ApiOperation("判断目标用户是否关注了当前用户")
    public ResponseResult isFollowMe(@RequestParam Integer userId,
                                     @RequestParam Integer fanId) {
        return apUserService.isFollowMe(userId, fanId);
    }

    @PostMapping("/avatar")
    @ApiOperation("用户头像上传")
    public ResponseResult uploadAvatar(@RequestParam("file") MultipartFile multipartFile) {
        Integer userId = ApUserThreadLocalUtil.getUser().getId();
        try {
            return apUserService.uploadAvatar(multipartFile, userId);
        } catch (IOException e) {
            log.error("头像上传异常", e);
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "文件上传异常");
        }
    }
    /**
     * 收藏或取消收藏文章
     * @param userId 用户ID
     * @param articleId 文章ID
     * @param isCollect 是否收藏 true-收藏 false-取消收藏
     * @return
     */
    @PostMapping("/collect")
    public ResponseResult collectArticle(@RequestParam Integer userId,
                                         @RequestParam Long articleId,
                                         @RequestParam Boolean isCollect) {
        return articleClient.collectArticle(userId, articleId, isCollect);
    }
    /**
     * 判断文章是否被收藏
     * @param userId 用户ID
     * @param articleId 文章ID
     * @return
     */
    @GetMapping("/isCollected")
    public ResponseResult isArticleCollected(@RequestParam Integer userId,
                                             @RequestParam Long articleId) {
        return articleClient.isArticleCollected(userId, articleId);
    }
    @GetMapping("/getwmuserinfo")
    public ResponseResult getWmUserInfo(@RequestParam Integer authorId) {
        ResponseResult wmUserInfo = wemediaClient.getWmUserInfo(authorId);
        Integer apUserId= (Integer) wmUserInfo.getData();
        return ResponseResult.okResult(apUserId);
    }
    @GetMapping("/{userId}/collections")
    @ApiOperation("获取用户收藏文章列表")
    public ResponseResult getUserCollections(@PathVariable Integer userId,
                                             @RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer size) {
        return apUserService.getUserCollections(userId, page, size);
    }

    /**
     * 获取用户点赞文章列表
     * @param userId 用户ID
     * @param page 页码
     * @param size 每页大小
     * @return
     */
    @GetMapping("/{userId}/likes")
    @ApiOperation("获取用户点赞文章列表")
    public ResponseResult getUserLikedArticles(@PathVariable Integer userId,
                                               @RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer size) {
        return apUserService.getUserLikedArticles(userId, page, size);
    }
    /**
     * 点赞或取消点赞文章
     * @param userId 用户ID
     * @param articleId 文章ID
     * @param isLike 是否点赞 true-点赞 false-取消点赞
     * @return
     */
    @PostMapping("/like")
    @ApiOperation("点赞或取消点赞文章")
    public ResponseResult likeArticle(@RequestParam Integer userId,
                                      @RequestParam Long articleId,
                                      @RequestParam Boolean isLike) {
        return apUserService.likeArticle(userId, articleId, isLike);
    }

    /**
     * 判断文章是否被点赞
     * @param userId 用户ID
     * @param articleId 文章ID
     * @return
     */
    @GetMapping("/isLiked")
    @ApiOperation("判断文章是否被点赞")
    public ResponseResult isArticleLiked(@RequestParam Integer userId,
                                         @RequestParam Long articleId) {
        return apUserService.isArticleLiked(userId, articleId);
    }

}